define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'business/privatesea/index',
                    add_url: 'business/privatesea/add',
                    edit_url: 'business/privatesea/edit',
                    recycle_url: 'business/privatesea/recycle',
                    info_url: 'business/privatesea/info',
                    del_url: 'business/privatesea/del',
                    table: 'business',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id'), sortable: true },
                        { field: 'nickname', title: __('Nickname'), operate: 'LIKE' },
                        { field: 'mobile', title: __('Mobile'), operate: 'LIKE' },
                        { field: 'email', title: __('Email'), operate: 'LIKE' },
                        { field: 'money', title: __('Money'), operate: 'LIKE' },
                        {
                            field: 'gender',
                            title: __('Gender'),
                            searchList: { '0': __('Gender 0'), '1': __('Gender 1'), '2': __('Gender 2') },
                            operate: 'FIND_IN_SET',
                            formatter: Table.api.formatter.normal
                        },
                        {
                            field: 'deal',
                            title: __('Deal'),
                            searchList: { '0': __('Deal 0'), '1': __('Deal 1') },
                            operate: 'FIND_IN_SET',
                            formatter: Table.api.formatter.normal
                        },
                        { field: 'source.name', title: __('SourceName'), operate: 'LIKE' },
                        { field: 'admin.nickname', title: __('AdminName'), operate: 'LIKE' },
                        { field: 'createtime', title: __('Createtime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'info',
                                    title: function (row) {
                                        return `${row.nickname}客户资料`
                                    },
                                    extend: 'data-toggle="tooltip"',
                                    icon: 'fa fa-bars',
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    url: $.fn.bootstrapTable.defaults.extend.info_url,
                                    extend: 'data-toggle=\'tooltip\' data-area= \'["100%", "100%"]\'', //重点是这一句
                                },
                                {
                                    name: 'recycle',
                                    text: '回收',
                                    title: function (row) {
                                        return `${row.nickname}客户回收`
                                    },
                                    extend: 'data-toggle="tooltip"',
                                    icon: 'fa fa-recycle',
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    url: $.fn.bootstrapTable.defaults.extend.recycle_url,
                                    confirm: '是否确认回收',
                                    success: function (data, ret) {
                                        $(".btn-refresh").trigger("click");
                                    },
                                    error: function (err) {
                                        console.log(err);
                                    }
                                },
                            ]

                        }
                    ]
                ]
            });
            // 回收，确认框的方法
            $('.btn-reduction').on('click', function () {
                let ids = Table.api.selectedids(table);
                ids = ids.toString()
                layer.confirm('确定要回收吗?', { title: '回收', btn: ['是', '否'] },
                    function (index) {
                        layer.close(index);
                        $.post("business/privatesea/recycle", { ids: ids, action: 'success', reply: '' }, function (response) {
                            if (response.code == 1) {
                                Toastr.success(response.msg)
                                $(".btn-refresh").trigger('click');
                            } else {
                                Toastr.error(response.msg)
                            }
                        }, 'json')
                    },
                    function (index) {
                        layer.close(index);
                    }
                );

            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        info: function () {
            //绑定事件
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                var panel = $($(this).attr("href"));
                if (panel.size() > 0) {
                    Controller.table[panel.attr("id")].call(this);
                    $(this).on('click', function (e) {
                        $($(this).attr("href")).find(".btn-refresh").trigger("click");
                    });
                }
            });

            //必须默认触发shown.bs.tab事件
            $('ul.nav-tabs li.active a[data-toggle="tab"]').trigger("shown.bs.tab");

            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        },
        table: {
            base: function () { },
            visit: function () {
                // 初始化表格参数配置
                Table.api.init({
                    extend: {
                        index_url: 'business/visit/index',
                        add_url: 'business/visit/add?ids=' + Fast.api.query('ids'),
                        edit_url: 'business/visit/edit',
                        del_url: 'business/visit/del',
                        table: 'business_visit',
                    }
                });
                var VisitTable = $("#VisitTable");

                // 初始化表格
                VisitTable.bootstrapTable({
                    url: 'business/visit/index?ids=' + Fast.api.query('ids'),
                    pk: 'id',
                    sortName: 'id',
                    toolbar: '#VisitToolbar',
                    columns: [
                        [
                            { checkbox: true },
                            { field: 'id', title: __('Id'), sortable: true },
                            { field: 'content', title: __('Content'), operate: 'LIKE' ,
                            // 限制字符长度，超出用省略号显示
                            formatter : function(value, row, index, field){
                                return ""+value+"";
                            },
                            cellStyle : function(value, row, index, field){
                                return {
                                    css: {"min-width": "120px",
                                        "white-space": "nowrap",
                                        "text-overflow": "ellipsis",
                                        "overflow": "hidden",
                                        "max-width":"300px"
                                    }
                                }
                            }
                            },
                            { field: 'admin.nickname', title: __('AdminName'), operate: 'LIKE' },
                            { field: 'createtime', title: __('Createtime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', },
                            { field: 'operate', title: __('Operate'), table: VisitTable, events: Table.api.events.operate, formatter: Table.api.formatter.operate }
                        ]
                    ]
                });

                // 为表格绑定事件
                Table.api.bindevent(VisitTable);
            },
            record: function () {
                // 初始化表格参数配置
                Table.api.init({
                    extend: {
                        index_url: 'business/record/index',
                        table: 'business_record',
                    }
                });

                var RecordTable = $("#RecordTable");

                // 初始化表格
                RecordTable.bootstrapTable({
                    url: 'business/record/index?ids=' + Fast.api.query('ids'),
                    pk: 'id',
                    sortName: 'id',
                    toolbar: '#RecordToolbar',
                    columns: [
                        [
                            { checkbox: true },
                            { field: 'id', title: __('Id'), sortable: true },
                            // { field: 'business.nickname', title: __('Nickname'), operate: 'LIKE' },
                            { field: 'total', title: __('Total'), operate: 'LIKE' },
                            { field: 'content', title: __('RContent'), operate: 'LIKE' },
                            { field: 'createtime', title: __('Createtime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange',  },
                            // { field: 'operate', title: __('Operate'), table: RecordTable, events: Table.api.events.operate, formatter: Table.api.formatter.operate }
                        ]
                    ]
                });

                // 为表格绑定事件
                Table.api.bindevent(RecordTable);
            },
            order: function () {
                // 初始化表格参数配置
                Table.api.init({
                    extend: {
                        index_url: 'business/order/index',
                        table: 'subject_order',
                    }
                });

                var OrderTable = $("#OrderTable");

                // 初始化表格
                OrderTable.bootstrapTable({
                    url: 'business/order/index?ids=' + Fast.api.query('ids'),
                    pk: 'id',
                    sortName: 'id',
                    toolbar: '#OrderToolbar',
                    columns: [
                        [
                            { checkbox: true },
                            { field: 'id', title: __('Id'), sortable: true },
                            { field: 'code', title: __('OCode'), operate: 'LIKE' },
                            { field: 'total', title: __('OTotal'), operate: 'LIKE' },
                            { field: 'subject.title', title: __('Subjectname'), operate: 'LIKE' },
                            { field: 'business.nickname', title: __('Nickname'), operate: 'LIKE' },
                            { field: 'createtime', title: __('Createtime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange',  },
                            // { field: 'operate', title: __('Operate'), table: OrderTable, events: Table.api.events.operate, formatter: Table.api.formatter.operate }
                        ]
                    ]
                });

                // 为表格绑定事件
                Table.api.bindevent(OrderTable);
            }
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        del: function () {
            Controller.api.bindevent();
        },
    };
    return Controller;
});
